import { RootDispatch } from "@src/store";

interface GlobalStateInterface {
  isLogin: boolean;
}
const state: GlobalStateInterface = {
  isLogin: false,
};
export default {
  name: "global",
  state,
  reducers: {
    login: (state: GlobalStateInterface, payload?: any): GlobalStateInterface => {
      state.isLogin = true;
      return state;
    },
    logout: (state: GlobalStateInterface, payload?: any): GlobalStateInterface => {
      state.isLogin = false;
      return state;
    },
  },
  // effects 可以是一个函数，也可以是一个对象
  // 还没研究过源码，猜测：是函数时可以派发其他 model 中的 action；是对象时只能派发当前 model 中的 action
  effects: (dispatch: RootDispatch) => ({
    async fetchLogin(payload?: any) {
      console.log("fetchLogin", payload);
      await new Promise<void>((resolve) => {
        setTimeout(() => {
          resolve();
        }, 1000);
      });
      this.login();
    },
    async fetchLogout(payload?: any) {
      console.log("fetchLogout", payload);
      await new Promise<void>((resolve) => {
        setTimeout(() => {
          resolve();
        }, 1000);
      });
      this.logout();
    },
  }),
  // effects: {},
};
